﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;

namespace WebQQ批量挂QQ软件
{
    public class RAR
    {
        /// <summary>
        /// 解压缩rar文件
        /// </summary>
        /// <param name="rarFilePath">rar文件路径</param>
        /// <param name="unRarPath">解压到文件夹</param>
        /// <param name="password">压缩包密码</param>
        /// <returns>异常信息</returns>
        public static string UnCompressRAR(string rarFilePath, string unRarPath, string password)
        {
            string errorMessage = string.Empty;

            try
            {
                //从注册表获取压缩包绝对路径
                RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                object the_Obj = the_Reg.GetValue("");
                string the_rar = the_Obj.ToString();
                the_Reg.Close();

                //没有解压缩文件夹创建文件夹
                if (Directory.Exists(unRarPath) == false)
                {
                    Directory.CreateDirectory(unRarPath);
                }

                string arg = string.Empty;//解压缩包命令
                if (!string.IsNullOrEmpty(password))
                    arg = "x -p" + password + " " + rarFilePath + " " + unRarPath + " -y";
                else
                    arg = "x " + rarFilePath + " " + unRarPath + " -y";

                ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = arg;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = the_rar.Remove(the_rar.LastIndexOf("\\"));//获取压缩包路径

                Process the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

        /// <summary>
        /// 打包成Rar，未测试
        /// </summary>
        /// <param name="path">要压缩文件路径</param>
        /// <param name="path">打包文件存放目录</param>
        /// <param name="rarName">压缩包文件名称</param>
        public string CompressRAR(string path, string rarPath, string rarName)
        {
            string errorMessage = string.Empty;

            try
            {
                RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                object the_Obj = the_Reg.GetValue("");
                string the_rar = the_Obj.ToString();
                the_Reg.Close();
                the_rar = the_rar.Substring(1, the_rar.Length - 7);
                
                //没有压缩包存放文件夹创建文件夹
                if (Directory.Exists(rarPath) == false)
                {
                    Directory.CreateDirectory(rarPath);
                }

                //命令参数
                //the_Info = "a " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
                string arg = "a " + rarName + " " + path + " -r"; ;
                ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = arg;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                //打包文件存放目录
                the_StartInfo.WorkingDirectory = rarPath;
                Process the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return errorMessage;
        }

    }
}
